package com.rjwh.dingdong.client.mvp.presenter.impl;

import android.content.Context;

import com.aishua.lib.task.AsResult;
import com.aishua.lib.utils.AsStrUtils;
import com.orangegangsters.github.swipyrefreshlayout.library.SwipyRefreshLayoutDirection;
import com.rjwh.dingdong.client.R;
import com.rjwh.dingdong.client.bean.jsonbean.ResGetHomeWorkListBean;
import com.rjwh.dingdong.client.bean.jsonbean.ResHomeworkAssignBean;
import com.rjwh.dingdong.client.bean.localbean.GetHomeWorkListBean;
import com.rjwh.dingdong.client.bean.localbean.HomeWorkListItem;
import com.rjwh.dingdong.client.bean.localbean.HomeworkAssignBean;
import com.rjwh.dingdong.client.constant.NetConstant;
import com.rjwh.dingdong.client.mvp.BasePresenter;
import com.rjwh.dingdong.client.mvp.model.impl.HomeWorkModel;
import com.rjwh.dingdong.client.mvp.presenter.IHomeWorkPresenter;
import com.rjwh.dingdong.client.mvp.view.HomeWorkView;
import com.rjwh.dingdong.client.util.ToastUtil;

import java.util.List;

/**
 * @author 王小珂
 * @ClassName HomeWorkPresenter
 * @Description
 * @date 2017-1-4
 * @history 2017-1-4 author: description:
 */

public class HomeWorkPresenter extends BasePresenter<HomeWorkView> implements IHomeWorkPresenter {

    private HomeWorkModel model;
    private SwipyRefreshLayoutDirection mDirection;

    public HomeWorkPresenter(Context context, HomeWorkView mMvpView) {
        super(context, mMvpView);
        model = new HomeWorkModel(this);
    }

    @Override
    public void onServerDataAcquired(int businessType, AsResult mAsResult, boolean isRefresh) {
        if (getMvpView() != null) {
            getMvpView().hideLoadDailog();
            switch (businessType) {
                case NetConstant.GETHOMEWORKLIST:
                    getMvpView().setRefreshing(false);
                    ResGetHomeWorkListBean resGetHomeWorkListBean = (ResGetHomeWorkListBean) mAsResult.getObj();
                    if (mAsResult.getResultCode() > NetConstant.MSG_CONNECT_SUCCESS && resGetHomeWorkListBean != null) {
                        GetHomeWorkListBean data = resGetHomeWorkListBean.getData();
                        if (data != null) {
                            List<HomeWorkListItem> homeworklist = data.getHomeworklist();
                            if (homeworklist != null) {
                                if (homeworklist.isEmpty()) {
                                    getMvpView().setDirection(SwipyRefreshLayoutDirection.TOP);
                                    if (mDirection == SwipyRefreshLayoutDirection.BOTTOM) {
                                        ToastUtil.showToast(getContext(), getContext().getString(R.string.empty_data_more));
                                    } else {
                                        ToastUtil.showToast(getContext(), getContext().getString(R.string.empty_data));
                                    }
                                } else {
                                    getMvpView().setDirection(SwipyRefreshLayoutDirection.BOTH);
                                }
                                getMvpView().loadData(isRefresh, homeworklist);
                            }
                        }
                    } else {
                        getMvpView().setDirection(SwipyRefreshLayoutDirection.TOP);
                        ToastUtil.showToast(getContext(), getContext().getString(R.string.refresh_error));
                    }

                    break;
                case NetConstant.HOMEWORK_ASSIGN:
                    ResHomeworkAssignBean resHomeworkAssignBean = (ResHomeworkAssignBean) mAsResult.getObj();
                    if (mAsResult.getResultCode() > NetConstant.MSG_CONNECT_SUCCESS && resHomeworkAssignBean != null) {
                        HomeworkAssignBean data = resHomeworkAssignBean.getData();
                        if (data != null) {
                            String actionurl = data.getActionurl();
                            if(!AsStrUtils.isEmpty(actionurl)){
                                getMvpView().loadAssignData(actionurl);
                            }
                        }
                    } else {
                        ToastUtil.showToast(getContext(), getContext().getString(R.string.refresh_error));
                    }

                    break;
                default:
                    break;
            }
        }
    }

    /**
     * 获取作业列表
     *
     * @param agecode
     * @param category
     * @param pageindex
     * @param isRefresh
     */
    @Override
    public void doGetHomeWorkList(int agecode, String category, int pageindex, boolean isRefresh, SwipyRefreshLayoutDirection direction) {
        if (isViewAttached()) {
            mDirection = direction;
            model.doGetHomeWorkList(agecode, category, pageindex, isRefresh);
        }
    }

    /**
     * 注册作业
     *
     * @param bookid
     * @param category
     */
    @Override
    public void doHomeWorkAssign(String bookid, String category) {
        if(isViewAttached()){
            getMvpView().showLoadDailog();
        }
        model.doHomeWorkAssign(bookid, category);
    }

    @Override
    public void detachView() {
        model.detachView();
        detachViews();
    }
}
